import axios from 'axios';
import fs from 'fs';

const fileId = '7ryM2G71XRVwkQnkklWrbI';
const nodeId = '0:2938'; // 启动页节点
const token = process.env.FIGMA_ACCESS_TOKEN;

console.log('🖼️ 重新获取启动页背景图...');

try {
  // 获取高质量背景图
  const imageResponse = await axios.get(`https://api.figma.com/v1/images/${fileId}`, {
    headers: { 'X-Figma-Token': token },
    params: {
      ids: nodeId,
      format: 'png',
      scale: 3, // 更高分辨率
      use_absolute_bounds: true
    }
  });

  const imageUrl = imageResponse.data.images[nodeId];
  if (imageUrl) {
    console.log('下载链接:', imageUrl);
    
    const imgResponse = await axios.get(imageUrl, { responseType: 'arraybuffer' });
    fs.writeFileSync('../entry/src/main/resources/rawfile/splash_background.png', imgResponse.data);
    console.log('✅ 高质量背景图已下载: splash_background.png');
    console.log('文件大小:', (imgResponse.data.length / 1024 / 1024).toFixed(2), 'MB');
  } else {
    console.log('❌ 无法获取图片下载链接');
  }

  // 尝试获取背景元素
  const nodeResponse = await axios.get(`https://api.figma.com/v1/files/${fileId}/nodes`, {
    headers: { 'X-Figma-Token': token },
    params: { ids: nodeId }
  });

  const nodeData = nodeResponse.data.nodes[nodeId];
  if (nodeData && nodeData.document && nodeData.document.children) {
    console.log('\n🔍 查找背景元素:');
    
    nodeData.document.children.forEach((child, index) => {
      console.log(`${index + 1}. ${child.name} (${child.type})`);
      
      // 查找可能的背景图片
      if (child.type === 'FRAME' && child.children) {
        child.children.forEach((subChild, subIndex) => {
          if (subChild.fills && subChild.fills.length > 0) {
            const fill = subChild.fills[0];
            if (fill.type === 'IMAGE') {
              console.log(`   找到图片: ${subChild.name} - ${fill.imageRef}`);
            }
          }
        });
      }
    });
  }

} catch (error) {
  console.log('❌ 获取失败:', error.response?.status, error.response?.statusText);
  console.log('错误详情:', error.response?.data);
}
